﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Nop.Services.Payments;

namespace Nop.Services.Logistics
{
    public static class ProcessPaymentRequestExtension
    {
        private const string logisticsPaymentNameIsPaid = "logisticsPaymentPaid";
        private const string logisticsPaymentNameType = "logisticsPaymentType";

        public static void SetAsPaid(this ProcessPaymentRequest processPaymentRequest)
        {
            processPaymentRequest.CustomValues[logisticsPaymentNameIsPaid] = true;
        }

        public static bool IsPaid(this ProcessPaymentRequest processPaymentRequest)
        {
            if (!processPaymentRequest.CustomValues.ContainsKey(logisticsPaymentNameIsPaid))
                return false;
            return (processPaymentRequest.CustomValues[logisticsPaymentNameIsPaid] as bool? == true);
        }

        public static bool TryUpdate(this ProcessPaymentRequest processPaymentRequest, FormCollection form)
        {
            processPaymentRequest.CreditCardNumber = form["ccNumber"];
            return true;
        }

        public static void SetPaymentType(this ProcessPaymentRequest processPaymentRequest, PaymentInfoType type)
        {
            processPaymentRequest.CustomValues[logisticsPaymentNameType] = (int)type;
        }

        public static PaymentInfoType GetPaymentType(this ProcessPaymentRequest processPaymentRequest)
        {
            var typeId = processPaymentRequest.CustomValues[logisticsPaymentNameType] as int?;

            if (typeId.HasValue && Enum.IsDefined(typeof(PaymentInfoType), typeId.Value))
                return (PaymentInfoType)typeId.Value;

            return PaymentInfoType.Unknown;
        }
    }

    public enum PaymentInfoType : int
    {
        Unknown = 0,
        ChangeDeliveryAddress = 1,
        ScheduleDelivery = 2
    }
}
